<?php
/***
 * $sql="SELECT op_value FROM ".$this->ecs->table('options')." WHERE op_key='appid'";
* $is_appid=$this->db->getOne($sql);
*/

class articleModel{
	private $db;
	private $ecs;
	public function __construct(){
		$this->db = $GLOBALS['db'];
		$this->ecs = $GLOBALS['ecs'];
	}

	public function get_article_list($cfg){
		$page = $cfg['page'];
		$where = " cat_id = ".$cfg['cat_id']." AND is_open=1 ";
		$pageSize = constant("PAGE_SIZE");
		$pageNum = ($page-1)*$pageSize;
		$sql="SELECT article_id,title,file_url,add_time,description FROM ".$this->ecs->table("article")." WHERE $where ORDER BY sort_order desc,add_time desc LIMIT ".$pageNum.",".$pageSize;
		$res = $this->db->getAll($sql);
		foreach ($res as $k=>$v){
			$res[$k]["add_time_show"] = date('Y-m-d',$v['add_time']);
		}
		$result['article_list']  = $res;
		return $result;
	}
	
	public function get_article_info($id){
		if ($id == 0){
			$id = $this->db->getOne ( "select article_id from " . $this->ecs->table ( 'article' ) . " where is_open = 1 order by sort_order" );
		}
		$article_row = $this->db->getRow('SELECT title, add_time,content,author,file_url FROM ' . $this->ecs->table('article') . '  WHERE article_id = ' . $id . ' AND cat_id > 0 AND is_open = 1');
		if (!empty($article_row)){
	
			$article_row['title'] = htmlspecialchars($article_row['title']);
				
			$article_row['add_time'] = date("Y-m-d",$article_row['add_time']);
				
			$article_row['file_url'] = "http://".$_SERVER['HTTP_HOST']."/".$article_row['file_url'];
	
			$replace_tag = array('<br />' , '<br/>' , '<br>' , '</p>');
	
			$article_row['content'] = str_replace('src="/images','src="http://'.$_SERVER['HTTP_HOST']."/images",$article_row['content']);
			$article_row['content'] = htmlspecialchars_decode(htmlspecialchars($article_row['content']));
	
			$article_row['content'] = str_replace($replace_tag, '{br}' , $article_row['content']);
	
			// 			$article_row['content'] = strip_tags($article_row['content']);
			$article_row['content'] = str_replace('{br}' , '<br />' , $article_row['content']);
	
			// 			$smarty->assign('article_data', $article_row);
	
		}
		return $article_row;
	}
}